package studyguide.ch09.customitem;

import java.io.IOException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class CustomItemMidlet extends MIDlet {

	private final Command exitCommand;
	private final Display display;
	
	private final Form form;
	
	private class SampleCommandListener implements CommandListener {

		public void commandAction(Command command, Displayable displayable) {
			if (command == exitCommand) {
				destroyApp(true);
				notifyDestroyed();
			} else {
				throw new RuntimeException("UNKNOWN COMMAND" + command);
			}
		}
	}
	
	private class CustomItemStateListener implements ItemStateListener {

		public void itemStateChanged(Item item) {
			if (item instanceof CustomItemB) {
				CustomItemB cib = (CustomItemB) item;
				System.out.println("Item B->Contents:[" + cib.getContents() + "]");
			} else if (item instanceof CustomItemD) {
				CustomItemD cid = (CustomItemD) item;
				System.out.println("Item D->current line:[" + cid.getCurrentLine() + "]");
			}
		}
		
	}
	
	public CustomItemMidlet() throws IOException {
		display = Display.getDisplay(this);
		form = new Form("Custom item form");
		
		// Assemble form:
		TextField textA = new TextField("Text A", "", 50, TextField.ANY);
		form.append(textA);
		
		CustomItemA customItemARed = new CustomItemA("Red Border:", 0x00ff0000);
		form.append(customItemARed);
		
		CustomItemA customItemADefault = new CustomItemA("Default Border:", display.getColor(Display.COLOR_BORDER));
		form.append(customItemADefault);
		
		TextField textB = new TextField("Text B", "", 50, TextField.ANY);
		form.append(textB);

		CustomItemB customItemB = new CustomItemB("Input component:");
		form.append(customItemB);
		
		TextField textC = new TextField("Text C", "", 50, TextField.ANY);
		form.append(textC);
		
		CustomItemC customItemC = new CustomItemC(
				"Simple traverse:", 
				display.getColor(Display.COLOR_BORDER),
				display.getColor(Display.COLOR_HIGHLIGHTED_BORDER));
		form.append(customItemC);
		
		TextField textD = new TextField("Text D", "", 50, TextField.ANY);
		form.append(textD);
		
		CustomItemD customItemD = new CustomItemD("Traverse:");
		form.append(customItemD);
		
		TextField textE = new TextField("Text E", "", 50, TextField.ANY);
		form.append(textE);
		
		exitCommand = new Command("Exit", Command.EXIT, 1);
		form.addCommand(exitCommand);
		SampleCommandListener listener = new SampleCommandListener();
		form.setCommandListener(listener);
		CustomItemStateListener itemListener = new CustomItemStateListener();
		form.setItemStateListener(itemListener);
	}
	
	protected void destroyApp(boolean unconditional) {
		// No Op
	}

	protected void pauseApp() {
		// No Op
	}

	protected void startApp() throws MIDletStateChangeException {
		display.setCurrent(form);
	}

}
